/**
 * Copyright (c) 2022 Presto Labs Pte. Ltd.
 * Author: Ju
 */

#include "coin2/controller/base_type.h"

#include "coin2/base/time.h"
namespace coin2::controller {

MemSlice SendHeartbeat(uint32_t node_id, std::string node_name,
                       std::string host_name, int64_t pid, int64_t last_seen,
                       bool need_reply, int64_t req_num) {
  json info;
  info["NodeId"] = node_id;
  info["NodeName"] = node_name;
  info["Hostname"] = host_name;
  info["LastSeen"] = last_seen;
  info["Pid"] = pid;
  info["Type"] = "strategy";

  json j;
  j["Info"] = info;
  j["NeedReply"] = need_reply;
  j["SeqNum"] = req_num;

  Packet pkt;
  pkt.version = 0;
  pkt.dst_id = DstType::Broadcast;
  pkt.src_id = node_id;
  pkt.encoding = MessageType::Msgpack;
  pkt.ttl = 0;
  pkt.type = PayloadType::Heartbeat;  // data
  pkt.len = j.size();
  pkt.payload = nlohmann::json::to_msgpack(j);

  base::Packer pk;
  pk << pkt;
  pk.Pack();
  return pk.TakeBuffer();
}

MemSlice MakeResponse(uint32_t src_id, uint32_t dst_id,
                      nlohmann::json payload) {
  Packet pkt;
  pkt.version = 0;
  pkt.dst_id = 0;
  pkt.src_id = src_id;
  pkt.encoding = MessageType::Msgpack;
  pkt.ttl = 0;
  pkt.type = PayloadType::APP;  // data
  pkt.len = payload.size();
  pkt.payload = nlohmann::json::to_msgpack(payload);

  base::Packer pk;
  pk << pkt;
  pk.Pack();
  return pk.TakeBuffer();
}

}  // namespace coin2::controller